<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>    
<html>
<head>
<base href="<%=basePath%>">
<script type="text/javascript" src="<%=path %>/js/json2.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>API_MANAGER</title>
    
</head>

<body>
<jsp:include page="/common/head.jsp"></jsp:include>
<div style="background:#edecec;">
			<div style="width:25%;float:left;height:auto;">
				<div>
					<jsp:include page="/tree.jsp"></jsp:include>
				</div>
			</div>
			<div id="contentDiv" style="width:75%;float:left;height:auto;">
				
			<form method="post" id="form1" action="<%=path%>/apiAction!update">
				<table >
					<tr><td>接口详情</td></tr>
					<tr><td>名称</td><td>
						${api.cname }
						<input type="hidden" id="apiname" name="apis.cname" value="${api.cname }" /> 
						<input type="hidden" id="apiid" name="apis.id" value="${api.id }" /> 
						<input type="hidden" id="aid" name="cate.aid" value="${cate.aid }" />
						<input type="hidden" id="pid" name="cate.pid" value="${cate.pid }" />	
					</td></tr>
					<tr><td>描述</td><td>${api.cdesc } </td></tr>
					<tr><td>开发人员</td><td>
							<c:forEach var="user" items="${users}">   
							      <c:if test="${user.id==api.devuser}">${user.niceName}</c:if>
							</c:forEach> 
 </td></tr>
					<tr><td>类型</td><td>
						${api.type}
					 </td></tr>
					<tr><td>请求URL</td><td><div id="url">${api.url }</div> 
					</td></tr>
					<tr><td>状态</td><td>
					
						<c:if test="${sessionScope.user.issuper == 1 }">
						<select name="apis.isok" id="apisisok" style="width:180px;">
							<option value="ok" <c:if test="${api.isok=='ok'}">selected="selected"</c:if>>接口文档撰写完成</option>
							<option value="no" <c:if test="${api.isok=='no'}">selected="selected"</c:if>>接口文档撰写没完成</option>
							<option value="dev" <c:if test="${api.isok=='dev'}">selected="selected"</c:if>>接口开发阶段</option>
							<option value="over" <c:if test="${api.isok=='over'}">selected="selected"</c:if>>接口开发完成</option>
							<option value="testno" <c:if test="${api.isok=='testno'}">selected="selected"</c:if>>口开发完成没有自测</option>
							<option value="testok" <c:if test="${api.isok=='testok'}">selected="selected"</c:if>>接口开发完成已经自测完成</option>
						</select>
							<input name="btss" type="button" value="保存状态" id="btss" />
						</c:if>
						<c:if test="${sessionScope.user.issuper == 0 }">
							<c:if test="${api.isok=='ok'}">接口文档撰写完成</c:if>
							<c:if test="${api.isok=='no'}">接口文档撰写没完成</c:if>
							<c:if test="${api.isok=='dev'}">接口开发阶段</c:if>
							<c:if test="${api.isok=='over'}">接口开发完成</c:if>
							<c:if test="${api.isok=='testno'}">口开发完成没有自测</c:if>
							<c:if test="${api.isok=='testok'}">接口开发完成已经自测完成</c:if>
						</c:if>
					</td></tr>
				</table>
				<br/>
				<div>
				<br>
				<br>请求参数列表<br>
					<table id="tab" border="1" width="100%">
				        <tr>
				            <td width="40px">序号</td>
				            <td>名称</td>
				            <td>描述</td>
				            <td>类型</td>
				            <td>备注</td>
				            <td>参数值</td>
				       </tr>
				       <c:forEach var="apipra" items="${apipralist }" varStatus="vst">
				       		<tr id="${vst.index+1}">
					            <td width="40px">${vst.index+1}</td>
					            <td><div class="reqname">${apipra.cname}</div></td>
					            <td>${apipra.cdesc}</td>
					            <td>${apipra.type}</td>
					            <td>${apipra.cbak}</td>
					            <td>
<%-- 					            	<input type="text" class="reqval" name="cname${vst.index}" id="cname${vst.index}" /> --%>
					            	
					            	<input type="text" class="reqval" name="value" id="cname${vst.index}" />
					            	
					            	<input type="hidden" class="reqval" name="field" value="${apipra.cname}"/>
					            	
					            	<input type="hidden" class="reqval" name="type" value="${apipra.type}"/>
					            	
					            </td>
				       		</tr>
				       </c:forEach>
				    </table>
				    <br>
					<br>响应参数列表
					<br>
					<textarea id="resp" readonly="readonly" rows="10" cols="200" style="max-width: 900px;overflow-y:visible"></textarea>
					<br><br>					
					<a href="javascript:void(0)" id="save2" class="easyui-linkbutton">发起请求</a>					
					<div style="color:red" id="message"></div>
					<br>
				</div>
				
					</form>
	</div>
	
	
</div>

<script type="text/javascript">
$(document).ready(function(){
	$("#jspname").val("test");
	$("#add1").click(function(){
		window.location.href="<%=path%>/apiAction!gotoapiadd?cate.aid="+$("#aid").val()+"&cate.pid="+$("#pid").val();
	});
	
	$("#go1").click(function(){
		window.location.href="<%=path%>/cateAction!cate?cate.pid="+$("#pid").val();
	});
	
	//<tr/>居中
    $("#tab tr").attr("align","center");
    
    $("#btss").click(function(){
    	var apiid = $("#apiid").val();
    	var aid = $("#aid").val();
    	var pid = $("#pid").val();
    	var isok = $("#apisisok").val();
    	$.messager.confirm('修改', '确定要修改接口状态吗？', function(r){
			if (r){
		    	$.post("<%=path%>/apiAction!updatestatus",{'api.id':apiid,'api.isok':isok},function(r){
					if(r == 'success'){
						window.location.href="<%=path%>/apiAction!gotoapiedit?cate.aid="+aid+"&cate.pid="+pid;
				    }else{
				    	//
				    }
				},"text");
			}
    	});
    });
    
    
	<%-- $("#save1").click(function(){
    	var url = $("#url").html()+"?";
    	var re = '';
    	var leg = $(".reqname").size();
    	$(".reqname").each(function(index){
    		var now_id = "#cname"+index;
    		re += $(this).html()+"="+$(now_id).val();
    		if(leg > 1 && index >= 0 && index < leg-1){
    			re += "&";
    		}
    	});
    	
    	$.ajax({ 
            type: "post", 
            url: "<%=path%>/apiAction!debugInterface", 
            dataType: "text",
             timeout : 30000,
             data:{'debugURL':url+re,'apiid':$('#apiid').val(),'apiname':$('#apiname').val()},
            success: function (data) {
            		if(data=="error"){
            			$("#resp").html("您的请求URL="+(url+re)+"\n\n接口请求失败，请检查接口参数或处理逻辑");
            		}else{
            			$("#resp").html("您的请求URL="+(url+re)+"\n\n"+data); 
            		}            		
            }, 
            error: function (XMLHttpRequest, textStatus, errorThrown) { 
            	$("#resp").html("您的请求URL="+(url+re)+"\n\n接口请求失败，请检查接口参数或处理逻辑"); 
            },
            complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
            	if(status=='timeout'){//超时,status还有success,error等值的情况
             		ajaxTimeoutTest.abort();
             		$("#resp").html("您的请求URL="+(url+re)+"\n\n接口请求失败，请检查接口参数或处理逻辑");
            	}
			}
   	 	});    	
	});  --%>

	//edit by caishx
	$("#save2").click(function(){
	var url = $("#url").html()
	$.ajax({ 
        type: "post", 
        url: "<%=path%>/apiAction!debugInterface2", 
        dataType: "text",
        timeout : 30000,
        data:$('#form1').serialize()+"&debugURL="+url,
        success: function (data) {
        		var result =  JSON.parse(data);
        		var request = JSON.stringify(result.request);
        		var response = JSON.stringify(result.response);
        		$("#resp").html("request:"+request+"\n\n"+"response:"+response);
        		
        		if(result.error=="error"){
        			$("#resp").html("您的请求URL="+(url+re)+"\n\n接口请求失败，请检查接口参数或处理逻辑");
        		}else{
        			$("#resp").html("request:"+request+"\n\n"+"response:"+response);
        		}            		
        }, 
        error: function (XMLHttpRequest, textStatus, errorThrown) { 
        	$("#resp").html("您的请求URL="+(url+re)+"\n\n接口请求失败，请检查接口参数或处理逻辑"); 
        },
        complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
        	if(status=='timeout'){//超时,status还有success,error等值的情况
         		ajaxTimeoutTest.abort();
         		$("#resp").html("您的请求URL="+(url+re)+"\n\n接口请求失败，请检查接口参数或处理逻辑");
        	}
		}
	 	});    	
});
    
});

</script>
</body>
</html>